<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountBroadcast;

class WechatOffiaccountBroadcastController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = "公众号群发";

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountBroadcast());

        $grid->model()->orderByDesc('id');
        $grid->column('id', __('laravel_admin_wechat.Id'))->hide();
        $grid->column('msg_id', __('laravel_admin_wechat.Msg id'));
        
        $grid->column('media_type', __('laravel_admin_wechat.Media type'))->using(['image'=>'图片','voice'=>'音频','video'=>'视频','news'=>'图文']);
        $grid->column('media_id', __('laravel_admin_wechat.Media id'));
        $grid->column('send_type', __('laravel_admin_wechat.Send type'))->using(['ALL'=>'所有人','TAG'=>'Tag','OPENID'=>'自定义筛选']);
        $grid->column('has_send', __('laravel_admin_wechat.Has send'))->bool();
        $grid->column('msg_status', __('laravel_admin_wechat.Msg status'));
        $grid->column('total_count', __('laravel_admin_wechat.Total count'));
        $grid->column('filter_count', __('laravel_admin_wechat.Filter count'));
        $grid->column('sent_count', __('laravel_admin_wechat.Sent count'));
        $grid->column('error_count', __('laravel_admin_wechat.Error count'));
        $grid->column('created_at', __('laravel_admin_wechat.Created at'));
        $grid->column('updated_at', __('laravel_admin_wechat.Updated at'));

		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			$actions->disableEdit();
			$actions->disableDelete();
			
			$actions->add(new \Liuhelong\LaravelAdmin\Wechat\Actions\WechatOffiaccountBroadcast\Del);
		});
		$grid->disableCreateButton();
		$grid->tools(function (Grid\Tools $tools) {
			$tools->append('<a class="btn btn-sm btn-info" style="margin-right:5px;" href="https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Batch_Sends_and_Originality_Checks.html#7" target="_blank"><i class="fa fa-question"></i>&nbsp;&nbsp;点我查看群发错误详细</a>');
		});
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountBroadcast::findOrFail($id));

        $show->field('id', __('laravel_admin_wechat.Id'));
        $show->field('msg_id', __('laravel_admin_wechat.Msg id'));
        $show->field('media_type', __('laravel_admin_wechat.Media type'));
        $show->field('media_id', __('laravel_admin_wechat.Media id'));
        $show->field('send_type', __('laravel_admin_wechat.Send type'));
        $show->field('msg_status',__('laravel_admin_wechat.Msg status'));
        $show->field('total_count', __('laravel_admin_wechat.Total count'));
        $show->field('filter_count', __('laravel_admin_wechat.Filter count'));
        $show->field('sent_count', __('laravel_admin_wechat.Sent count'));
        $show->field('error_count', __('laravel_admin_wechat.Error count'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));
        $show->field('updated_at', __('laravel_admin_wechat.Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountBroadcast());

        $form->text('msg_id', __('laravel_admin_wechat.Msg id'));
        $form->text('media_type', __('laravel_admin_wechat.Media type'));
        $form->text('media_id', __('laravel_admin_wechat.Media id'));
        $form->text('send_type', __('laravel_admin_wechat.Send type'));
        $form->text('msg_status',__('laravel_admin_wechat.Msg status'));
        $form->number('total_count', __('laravel_admin_wechat.Total count'));
        $form->number('filter_count', __('laravel_admin_wechat.Filter count'));
        $form->number('sent_count', __('laravel_admin_wechat.Sent count'));
        $form->number('error_count', __('laravel_admin_wechat.Error count'));

        return $form;
    }
}
